#!/usr/bin/perl -w
#
#    s3webalizer - converts s3 access logs into apache combined logs readable by webalizer
#    Copyright (C) 2008 Gil Megidish
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;

# get a list of all log files in the current directory
my @files = glob("access_log-*");

foreach my $file (sort(@files))
{
	open(FD, "<$file") || die("can't open $file for reading\n");
	while (my $s = <FD>)
	{
		chomp $s;

		if ($s =~ /(\w+) (\w+) \[([^\]]+)\] ([\d\.]+) (\w+) (\w+) ([\w\.]+) ([^\s]+) "([^"]+)" (\d+) ([^\s]+) (\d+) (\d+) (\d+) (\d+) "([^"]+)" "([^"]+)"/)
		{
			# log line parsed successfully
			my $bucket_owner = $1;
			my $bucket = $2;
			my $time = $3;
			my $remote_ip = $4;
			my $requestor = $5;
			my $request_id = $6;
			my $operation = $7;
			my $key = $8;
			my $request_uri = $9;
			my $http_status = $10;
			my $error_code = $11;
			my $bytes_sent = $12;
			my $object_size = $13;
			my $total_time = $14;
			my $turn_around_time = $15;
			my $referrer = $16;
			my $user_agent = $17;

			if ($error_code eq "-")
			{
				# no s3 error 
				print "$remote_ip - - [$time] \"$request_uri\" $http_status $bytes_sent \"$referrer\" \"$user_agent\"\n";
			}
		}
	}

	close(FD);
}

